﻿using HML.Design;
using System;
using System.Collections;
using System.ComponentModel;

namespace HML
{
    /// <summary>
    ///DropDownListPlus选项集合
    /// </summary>
    public sealed class DropDownListPlusItemCollection : IList, ICollection, IEnumerable
    {
        #region 属性

        public DropDownListPlus Owner { get { return this.owner; } }

        #endregion

        #region

        private ArrayList dropDownListPlusItemList = new ArrayList();

        private DropDownListPlus owner = null;

        #endregion

        public DropDownListPlusItemCollection()
        {

        }

        #region IEnumerable

        public IEnumerator GetEnumerator()
        {
            DropDownListPlusItem[] listArray = new DropDownListPlusItem[this.dropDownListPlusItemList.Count];
            for (int index = 0; index < listArray.Length; ++index)
            {
                listArray[index] = (DropDownListPlusItem)this.dropDownListPlusItemList[index];
            }
            return listArray.GetEnumerator();
        }

        #endregion

        #region ICollection

        public void CopyTo(Array array, int index)
        {
            for (int i = 0; i < this.Count; i++)
            {
                array.SetValue(this.dropDownListPlusItemList[i], i + index);
            }
        }

        public int Count
        {
            get { return this.dropDownListPlusItemList.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return (object)this; }
        }

        #endregion

        #region IList

        public int Add(object value)
        {
            if (!(value is DropDownListPlusItem))
            {
                throw new ArgumentException("DropDownListPlusItem");
            }
            return this.Add((DropDownListPlusItem)value);
        }

        public int Add(DropDownListPlusItem item)
        {
            this.ValidBind();
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (item.Owner != null)
            {
                if (item.Owner == this)
                {
                    throw new ArgumentException(" DropDownListPlusItem 不允许重复性添加到当前集合");
                }

                throw new ArgumentException(" DropDownListPlusItem 已经被添加到其他集合中");
            }
            item.Owner = this;

            this.dropDownListPlusItemList.Add(item);
            return this.Count - 1;
        }

        public void Clear()
        {
            this.ValidBind();
            for (int i = 0; i < this.dropDownListPlusItemList.Count; i++)
            {
                ((DropDownListPlusItem)this.dropDownListPlusItemList[i]).Owner = null;
            }
            this.dropDownListPlusItemList.Clear();
        }

        public bool Contains(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            return this.IndexOf(value) != -1;
        }

        bool IList.Contains(object item)
        {
            if (item is DropDownListPlusItem)
            {
                return this.Contains((DropDownListPlusItem)item);
            }
            return false;
        }

        public int IndexOf(object item)
        {
            if (item is DropDownListPlusItem)
            {
                return this.dropDownListPlusItemList.IndexOf(item);
            }
            return -1;
        }

        public BreadcrumbBarItem Find(int index)
        {
            if (index < 0 || index >= this.Count)
            {
                return null;
            }
            return (BreadcrumbBarItem)this.dropDownListPlusItemList[index];
        }

        public void Insert(int index, object value)
        {
            this.ValidBind();
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is DropDownListPlusItem))
            {
                throw new ArgumentException("DropDownListPlusItem");
            }
            DropDownListPlusItem item = (DropDownListPlusItem)value;
            if (item.Owner != null)
            {
                if (item.Owner == this)
                {
                    throw new ArgumentException(" DropDownListPlusItem 不允许重复性添加到当前集合");
                }

                throw new ArgumentException(" DropDownListPlusItem 已经被添加到其他集合中");
            }
            item.Owner = this;

            if (index > this.Count)
            {
                index = this.Count;
            }
            ArrayList tmp = new ArrayList();
            for (int i = 0; i < index; i++)
            {
                tmp.Add(this.dropDownListPlusItemList[i]);
            }
            tmp.Add(value);
            for (int i = index; i < this.Count; i++)
            {
                tmp.Add(this.dropDownListPlusItemList[i]);
            }
            this.dropDownListPlusItemList = tmp;
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            if (!(value is DropDownListPlusItem))
            {
                throw new ArgumentException("DropDownListPlusItem");
            }
            this.Remove((DropDownListPlusItem)value);
        }

        public void Remove(DropDownListPlusItem item)
        {
            this.ValidBind();
            item.Owner = null;
            this.dropDownListPlusItemList.Remove(item);
        }

        public void RemoveAt(int index)
        {
            this.ValidBind();
            ((DropDownListPlusItem)this.dropDownListPlusItemList[index]).Owner = null;
            this.dropDownListPlusItemList.RemoveAt(index);
        }

        public DropDownListPlusItem this[int index]
        {
            get { return (DropDownListPlusItem)this.dropDownListPlusItemList[index]; }
            set
            {
                this.ValidBind();
                ((DropDownListPlusItem)this.dropDownListPlusItemList[index]).Owner = null;
                this.dropDownListPlusItemList[index] = value;
            }
        }

        object IList.this[int index]
        {
            get { return (object)this.dropDownListPlusItemList[index]; }
            set
            {
                this.ValidBind();
                ((DropDownListPlusItem)this.dropDownListPlusItemList[index]).Owner = null;
                this.dropDownListPlusItemList[index] = value;
            }
        }

        #endregion

        #region 方法

        internal void SetOwner(DropDownListPlus owner)
        {
            this.owner = owner;
        }

        private void ValidBind()
        {
            if (this.owner != null)
            {
                throw new Exception("不允许修改绑定后的数据源,请用“BindItemsSource”方法更新数据源");
            }
        }

        #endregion

    }

}
